package com.view.loadmore;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.view.BuildConfig;

import java.util.List;

/**
 * 加载更多适配器
 */
public abstract class LoadMoreAdapter extends RecyclerView.Adapter {

    private static final String TAG = "LoadMoreAdapter";
    /**
     * 加载更多条目类型
     */
    public static final int TYPE_LOADMORE = -100;

    protected LoadMoreRecyclerView mLoadMoreRecyclerview;

    private RecyclerView.LayoutManager layoutManager;

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        mLoadMoreRecyclerview = (LoadMoreRecyclerView) recyclerView;
        layoutManager = mLoadMoreRecyclerview.getLayoutManager();
        //解决网格占一整行
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int itemViewType = getItemViewType(position);
                    return itemViewType == TYPE_LOADMORE ? gridLayoutManager.getSpanCount() : 1;
                }
            });
        }
    }

    @Override
    public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        //解决瀑布流加载更多占一整行
        int itemViewType = holder.getItemViewType();
        if (itemViewType == TYPE_LOADMORE) {
            ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
            if (layoutParams == null) {
                StaggeredGridLayoutManager.LayoutParams staggerLayoutParams =
                        new StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                ViewGroup.LayoutParams.WRAP_CONTENT);
                staggerLayoutParams.setFullSpan(true);
                holder.itemView.setLayoutParams(layoutParams);
            } else if (layoutParams instanceof StaggeredGridLayoutManager.LayoutParams) {
                ((StaggeredGridLayoutManager.LayoutParams) layoutParams).setFullSpan(true);
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mGetItemCount() > 0 && position == getItemCount() - 1) return TYPE_LOADMORE;
        if (super.getItemViewType(position) == TYPE_LOADMORE) {
            if (BuildConfig.DEBUG) {
                throw new IllegalArgumentException(TYPE_LOADMORE + " has been declared to load more.Please chose anther number.");
            } else {
                Log.w(TAG, TYPE_LOADMORE + " has been declared to load more.Please chose anther number.");
            }
        }
        return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_LOADMORE) {
            return new LoadmoreViewHolder(mLoadMoreRecyclerview.getLoadMoreView());
        } else {
            return mOnCreateViewHolder(parent, viewType);
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
        if (viewHolder.getItemViewType() == TYPE_LOADMORE) {
            return;
        }
        mOnBindViewHolder(viewHolder, position);
    }

    @Override
    public int getItemCount() {
        if (mGetItemCount() > 0) {
            return 1 + mGetItemCount();
        } else {
            return 0;
        }
    }

    public abstract int mGetItemCount();

    static class LoadmoreViewHolder extends RecyclerView.ViewHolder {
        LoadMoreView loadMoreView;

        public LoadmoreViewHolder(LoadMoreView itemView) {
            super(itemView);
            this.loadMoreView = itemView;
        }
    }

    /**
     * @param parent
     * @param viewType
     * @return
     * @see RecyclerView.Adapter#onCreateViewHolder(ViewGroup, int)
     */
    protected abstract RecyclerView.ViewHolder mOnCreateViewHolder(@NonNull ViewGroup parent, int viewType);

    /**
     * @param viewHolder
     * @param position
     * @see RecyclerView.Adapter#onBindViewHolder(RecyclerView.ViewHolder, int, List)
     */
    protected abstract void mOnBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position);
}